package com.flong.spring.conditional;

import com.flong.spring.bean.RainBow;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.RootBeanDefinition;
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
import org.springframework.core.type.AnnotationMetadata;

/**
 * @Author:liangjl
 * @Date: 2020/6/10-21:06
 * @Eamil:jilongliang@sina.com
 * @Description:
 */
public class MyImportBeanDefinitionRegistrar  implements ImportBeanDefinitionRegistrar {


    /**
     * @see AnnotationMetadata 当前类的注解信息
     * @see BeanDefinitionRegistry BeanDefinition 注册类，把所有需要添加到容器中的Bean调用
     * @see BeanDefinitionRegistry#registerBeanDefinition 手工注册进来
     *
     * @param importingClassMetadata
     * @param registry
     */
    @Override
    public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {

        boolean red = registry.containsBeanDefinition("com.flong.spring.bean.Red");
        boolean blue = registry.containsBeanDefinition("com.flong.spring.bean.Blue");
        if (red && blue){
            //指定bean名
            RootBeanDefinition rootBeanDefinition = new RootBeanDefinition(RainBow.class);
            registry.registerBeanDefinition("rainBow",rootBeanDefinition);

        }

    }
}
